<?php

/**
 * Template Name: user_gamelist
 *
 * date:2011/4/11 
 * @author Ryan 
 * 
 */


get_header();
$templateurl = get_bloginfo('template_url');

$user = $_SESSION['userdata'];
$selfLink = $_SERVER['REQUEST_URL'];

if ($user){
	
	$type = $_GET['type'];
	$action = $_GET['action'];
	if ($action == 'delete'){
		$id = $_GET['id'];
		deleteUserGames($id);
	}
	$page = $_GET['page'];
	if (!$page){
		$page = 1;
	}
	$game_type = $type == 'opened' ? 0 : 1;
	
	
	$results = getUserGames($user->ID, $game_type, $page);
	
	
	$gamecount = getUserGamesCount($user->ID, $game_type);
	
	
	$gamelist = array();
	
	foreach ($results as $result){
		$gamelist[] = array($result, get_post($result->gameid));
	}
	
	
?>
	<div id="UC_left" class="UC_content">
		<span id="ucent"><samp></samp>用户中心</span>
		<ul>
			<li class="UC_no"><a href="/user_profile">个人信息</a></li>
			<li <?php echo $type == 'opened' ? 'id="UC_xuanding"' : 'class="UC_no"'; ?> ><a href="/user_openedgame">玩过的游戏</a></li>
			<li <?php echo $type == 'opened' ? 'class="UC_no"' : 'id="UC_xuanding"'; ?>><a href="/user_collectedgame">收藏的游戏</a></li>
		</ul>
		<img src="<?php echo $templateurl;?>/images/usercenter_tc.gif" id="UCL_bg" alt="游戏搜搜用户中心" title="游戏搜搜用户中心" />
		<img src="<?php echo $templateurl;?>/images/usercenter_tr.gif" id="UCL_cr" alt="游戏搜搜用户中心" title="游戏搜搜用户中心" />
	</div>
	<div id="UC_right" class="UC_content">
		<span class="ucright_sider"><?php echo $type == 'opened' ? '玩过的游戏' : '收藏的游戏'; ?></span>
		<div class="ucright_cont palygame" >
			 <table align="left">
			 	<thead align="left">
					<th width="84%" valign="middle" align="left">游戏名称</th>
					<th width="15%" valign="middle" align="left" class="usercaozuo">操作</th>
				</thead>
				<tbody>
					<?php foreach ($gamelist as $game){?>
					<tr>
						<td><a href="/?p=<?php echo $game[1]->ID;?>" target="_blank"><?php echo $game[1]->post_title; ?></a></td>
						<td>
							<a href="<?php echo $selfLink.'?action=delete&id='.$game[0]->ID; ?>" class="deletegame" >删除</a>
						</td>
					</tr>
					<?php }?>
				</tbody>
			 </table>
		</div>
		<div id="scroll">
			<?php
				$pages = (int)Math.ceil($gamecount/20);
				
				if ($page != 1){
			?>
			<a href="<?php echo $selfLink.'page=1'?>" ><img src="images/next-top.gif" alt="上一页" title="上一页" class="f_l"/></a>
			<?php
				}
				for ($p = 1; $p <=$pages ; $p++){
					if($p == $page){
			?>
				<font class="linknone"><?php echo $p;?></font>
			<?php 
					}else{
			?>
				<a href="<?php echo $selfLink.'page='.$p; ?>" class="linkstyle03"><?php echo $p;?></a>
			<?php
					}
				}
				
				if($page != $pages && $pages != 0){
			?>
				<a href="<?php echo $selfLink.'page='.($page+1); ?>" ><img src="images/next-bottom.gif" alt="下一页" title="下一页" class="f_l" /></a>
			<?php
				}
			
			?>
			
		</div>
		
		
		
	</div>
<?php }else {
	header("Location: ".get_bloginfo('home').'/login');
}
?>
	
<?php get_footer();?>